package com.ruoyi.web.controller.lims;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lims.domain.Barcode;
import com.ruoyi.lims.domain.LimsTaskIssued;
import com.ruoyi.lims.service.ILimsTaskIssuedService;
import com.ruoyi.lims.service.LimsBarcodeComputerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 任务下发Controller
 *
 * @author wry
 * @date 2023-07-11
 */
@RestController
@RequestMapping("/lims/issued")
public class LimsTaskIssuedController extends BaseController
{
    @Autowired
    private ILimsTaskIssuedService limsTaskIssuedService;
    @Autowired
    private LimsBarcodeComputerService limsBarcodeComputerService;

    /**
     * 查询任务下发列表
     */
    @PreAuthorize("@ss.hasPermi('lims:issuedChild:list')")
    @GetMapping("/list")
    public TableDataInfo list(LimsTaskIssued limsTaskIssued)
    {
        if("0".equals(limsTaskIssued.getStatus())){
          limsTaskIssued.setStatus(null);
        }
        if("未下发".equals(limsTaskIssued.getDownStatus())){
            limsTaskIssued.setDownStatus("1");
        }
        startPage();
        if("like".equals(limsTaskIssued.getSearchCondition())){
           if("sampleId".equals(limsTaskIssued.getSearchField())){
               limsTaskIssued.setSampleIdLike(limsTaskIssued.getSearchValueLike());
           }else   if("sampleName".equals(limsTaskIssued.getSearchField())){
                limsTaskIssued.setSampleNameLike(limsTaskIssued.getSearchValueLike());
           }else   if("libraryNumber".equals(limsTaskIssued.getSearchField())){
                limsTaskIssued.setLibraryNumberLike(limsTaskIssued.getSearchValueLike());
            }
        }else if("in".equals(limsTaskIssued.getSearchCondition())){
            if("sampleId".equals(limsTaskIssued.getSearchField())){
                limsTaskIssued.setSampleIdIn(limsTaskIssued.getSearchValueIn());
            }else  if("sampleName".equals(limsTaskIssued.getSearchField())){
                limsTaskIssued.setSampleNameIn(limsTaskIssued.getSearchValueIn());
            }else  if("libraryNumber".equals(limsTaskIssued.getSearchField())){
                limsTaskIssued.setLibraryNumberIn(limsTaskIssued.getSearchValueIn());
            }
        }
        List<LimsTaskIssued> list = limsTaskIssuedService.selectLimsTaskIssuedList(limsTaskIssued);
        return getDataTable(list);
    }


    @Log(title = "任务下发管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('lims:issuedChild:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<LimsTaskIssued> util = new ExcelUtil<LimsTaskIssued>(LimsTaskIssued.class);
        List<LimsTaskIssued> taskList = util.importExcel(file.getInputStream());
        String message = limsTaskIssuedService.importLimsTaskIssued(taskList, updateSupport, getUsername());
        return success(message);
    }


    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<LimsTaskIssued> util = new ExcelUtil<LimsTaskIssued>(LimsTaskIssued.class);
        util.importTemplateExcel(response, "任务下发数据");
    }
    /**
     * 导出任务下发列表
     */
    @PreAuthorize("@ss.hasPermi('lims:issuedChild:export')")
    @Log(title = "任务下发", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LimsTaskIssued limsTaskIssued)
    {
        if("0".equals(limsTaskIssued.getStatus())){
            limsTaskIssued.setStatus(null);
        }
        if("未下发".equals(limsTaskIssued.getDownStatus())){
            limsTaskIssued.setDownStatus("1");
        }
        if("like".equals(limsTaskIssued.getSearchCondition())){
            if("sampleId".equals(limsTaskIssued.getSearchField())){
                limsTaskIssued.setSampleIdLike(limsTaskIssued.getSearchValueLike());
            }else   if("sampleName".equals(limsTaskIssued.getSearchField())){
                limsTaskIssued.setSampleNameLike(limsTaskIssued.getSearchValueLike());
            }else   if("libraryNumber".equals(limsTaskIssued.getSearchField())){
                limsTaskIssued.setLibraryNumberLike(limsTaskIssued.getSearchValueLike());
            }
        }else if("in".equals(limsTaskIssued.getSearchCondition())){
            if("sampleId".equals(limsTaskIssued.getSearchField())){
                limsTaskIssued.setSampleIdIn(limsTaskIssued.getSearchValueIn());
            }else  if("sampleName".equals(limsTaskIssued.getSearchField())){
                limsTaskIssued.setSampleNameIn(limsTaskIssued.getSearchValueIn());
            }else  if("libraryNumber".equals(limsTaskIssued.getSearchField())){
                limsTaskIssued.setLibraryNumberIn(limsTaskIssued.getSearchValueIn());
            }
        }
        List<LimsTaskIssued> list = limsTaskIssuedService.selectLimsTaskIssuedList(limsTaskIssued);
        ExcelUtil<LimsTaskIssued> util = new ExcelUtil<LimsTaskIssued>(LimsTaskIssued.class);
        util.exportExcel(response, list, "任务下发数据");
    }

    /**
     * 获取任务下发详细信息
     */
    @PreAuthorize("@ss.hasPermi('lims:issuedChild:query')")
    @GetMapping(value = "/{issuedId}")
    public AjaxResult getInfo(@PathVariable("issuedId") Long issuedId)
    {
        return success(limsTaskIssuedService.selectLimsTaskIssuedByIssuedId(issuedId));
    }

    /**
     * 新增任务下发
     */
    @PreAuthorize("@ss.hasPermi('lims:issuedChild:add')")
    @Log(title = "任务下发", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LimsTaskIssued limsTaskIssued)
    {
        return toAjax(limsTaskIssuedService.insertLimsTaskIssued(limsTaskIssued));
    }

    /**
     * 修改任务下发
     */
    @PreAuthorize("@ss.hasPermi('lims:issuedChild:edit')")
    @Log(title = "任务下发", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LimsTaskIssued limsTaskIssued)
    {
        return toAjax(limsTaskIssuedService.updateLimsTaskIssued(limsTaskIssued));
    }

    /**
     * 删除任务下发
     */
    @PreAuthorize("@ss.hasPermi('lims:issuedChild:remove')")
    @Log(title = "任务下发", businessType = BusinessType.DELETE)
	@DeleteMapping("/{issuedIds}")
    public AjaxResult remove(@PathVariable Long[] issuedIds)
    {
        return toAjax(limsTaskIssuedService.deleteLimsTaskIssuedByIssuedIds(issuedIds));
    }

    @Log(title = "任务下发管理-Barcode计算", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('lims:issuedChild:import')")
    @PostMapping("/importBarcode")
    public void importBarcode(MultipartFile file,HttpServletResponse response) throws Exception
    {
        ExcelUtil<Barcode> util = new ExcelUtil<Barcode>(Barcode.class);
        List<Barcode> list = util.importExcel(file.getInputStream());
        limsBarcodeComputerService.getBarcode(list,response);
    }

    @Log(title = "任务下发管理-下发")
    @PreAuthorize("@ss.hasPermi('lims:issuedChild:down')")
    @PostMapping("/downIssued")
    public AjaxResult downIssued(@RequestBody Map<String, Object> data) throws Exception
    {
        return limsTaskIssuedService.downIssued(data);
    }

}
